package design;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;

import com.sun.xacml.Obligation;
import com.sun.xacml.ctx.ResponseCtx;
import com.sun.xacml.ctx.Result;
import com.sun.xacml.ctx.Status;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JTextArea;

public class ViewResponse extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtResource;
	private JTextField txtDecision;
	private JLabel lblStatus;
	private JLabel lblCode;
	private JLabel lblMessage;
	private JTextField txtMessage;
	private JLabel lblObligations;
	private JButton btnOk;
	private JTextArea txtCode;
	/**
	 * Create the frame.
	 * 
	 */
	public void init() {		
		setTitle("View Response");		
		setBounds(100, 100, 423, 294);
		this.setLocationRelativeTo(getParent());
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblResult = new JLabel("RESULT");
		lblResult.setFont(new Font("Times New Roman", Font.BOLD, 16));
		lblResult.setBounds(20, 24, 102, 30);
		contentPane.add(lblResult);

		JLabel lblResourceId = new JLabel("Resource ID :");
		lblResourceId.setFont(new Font("Times New Roman", Font.BOLD, 12));
		lblResourceId.setBounds(20, 76, 72, 14);
		contentPane.add(lblResourceId);

		txtResource = new JTextField();
		txtResource.setBounds(99, 73, 295, 23);
		contentPane.add(txtResource);
		txtResource.setColumns(10);

		JLabel lblDecision = new JLabel("Decision :");
		lblDecision.setFont(new Font("Times New Roman", Font.BOLD, 12));
		lblDecision.setBounds(20, 112, 63, 14);
		contentPane.add(lblDecision);

		txtDecision = new JTextField();
		txtDecision.setBounds(99, 109, 295, 23);
		contentPane.add(txtDecision);
		txtDecision.setColumns(10);

		lblStatus = new JLabel("Status ");
		lblStatus.setFont(new Font("Times New Roman", Font.BOLD, 12));
		lblStatus.setBounds(20, 147, 46, 14);
		contentPane.add(lblStatus);

		lblCode = new JLabel("Code :");
		lblCode.setFont(new Font("Times New Roman", Font.PLAIN, 12));
		lblCode.setBounds(37, 176, 46, 14);
		contentPane.add(lblCode);

		lblMessage = new JLabel("Message :");
		lblMessage.setFont(new Font("Times New Roman", Font.PLAIN, 12));

		txtMessage = new JTextField();
		
		lblObligations = new JLabel("Obligations :");
		lblObligations.setFont(new Font("Times New Roman", Font.BOLD, 12));

		new JTextField();

		btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});

		btnOk.setFont(new Font("Times New Roman", Font.PLAIN, 12));
		btnOk.setBounds(99, 206, 89, 23);
		contentPane.add(btnOk);

		txtCode = new JTextArea(5, 10);
	
		JScrollPane scrollPane = new JScrollPane(txtCode);
		scrollPane.setBounds(99, 168, 295, 35);
		contentPane.add(scrollPane);				
	}

	public ViewResponse(ResponseCtx response){
		super("View Response");
		init();
		
		Set results = response.getResults();
		// Go through all results
		Iterator it = results.iterator();
		String resource = null;
		Status status = null;
		Set obligations = null;
		int decision;
		while (it.hasNext()) {
			Result result = (Result) (it.next());
			resource = result.getResource();
			decision = result.getDecision();
			status = result.getStatus();
			obligations = result.getObligations();

			if (resource != null)
				txtResource.setText(resource);
			else
				txtResource.setText("No");
			
			txtDecision.setText(result.DECISIONS[decision]);

			if (status != null) {
				String message = status.getMessage();
				Iterator codeList = status.getCode().iterator();
				String code = (String) (codeList.next());
				if (codeList.hasNext()) {
					while (codeList.hasNext()) {
						txtCode.append(code + "\n");
						code = (String) (codeList.next());
					}
				} else
					txtCode.setText(code );
				if (message == null) {
					// co obligation,ko message
					if (obligations.size() > 0) {
						Iterator it1 = obligations.iterator();
						lblObligations.setBounds(20, 211 , 72, 14);
						contentPane.add(lblObligations);
						int i = 0;
						while (it1.hasNext()) {
							Obligation obligation = (Obligation) (it1.next());
							JTextField txtObligations = new JTextField();
							txtObligations.setBounds(99, 211 + i*38, 295, 23);
							txtObligations.setText(obligation.getId().toString());
							contentPane.add(txtObligations);
							i++;
						}
						setBounds(100,100 ,423 ,342 + 38*(i-1));
						btnOk.setBounds(99, 256 + 38 * (i - 1), 89, 23);						
					}
				} else {
					txtMessage.setText(message);
					if (obligations.size() == 0) {
						setBounds(100, 100, 423, 342);
						btnOk.setBounds(99, 256, 89, 23);
						lblMessage.setBounds(37, 210, 55, 14);
						txtMessage.setBounds(99, 208, 295, 23);
						txtMessage.setColumns(10);
						contentPane.add(lblMessage);
						contentPane.add(txtMessage);
					} 
				}
			}			
		}
	}
}
